<template>
  <!--游客页-->
  <div class="partal">
    <div class="partal_top">
      <img src="../../../static/image/log.png" />
      <span>商学院客户分流</span>
    </div>
    <div class="partal_subject">
      <img src="../../../static/image/people.png" />
    </div>
    <div class="caozuo">
      <button class="enter" @click="wxLoginBtn()">进入</button>
      <span @click="Acclogin()">账号密码登录</span>
    </div>
  </div>
</template>
<script>
import { wxLogin, codeData } from "../../api/api";
import querystring from "query-string";
import axios from "axios";
import { Toast, Indicator } from "mint-ui";

export default {
  name: "Portalpage",
  data() {
    return {};
  },
  created() {
    let href = window.location.href;
    if (href.includes("/?code")) {
      //url包括 com/?code 证明为从微信跳转回来的
      var url = href.substring(0, href.length - 2); //vue自动在末尾加了 #/ 符号，截取去掉
      var jingPosit = url.indexOf("/") + 22; //获取域名结束的位置
      var urlLeft = url.substring(0, jingPosit); //url左侧部分
      var urlRight = url.substring(jingPosit, url.length); //url右侧部分
      window.location = urlLeft + "#/" + urlRight; //拼接跳转
      if (this.$route.query.code) {
        this.getDa(this.$route.query.code);
      }
    }
    if (localStorage.getItem("token")) {
      if (localStorage.getItem("user")) {
        let newUser = JSON.parse(localStorage.getItem("user"));
        if (newUser.roleCode === "salesman") {
          this.$router.push({ name: "Sahomepage" });
        }
        if (newUser.roleCode === "center") {
          this.$router.push({ name: "Ghome" });
        }
        if (newUser.roleCode === "branch") {
          this.$router.push({ name: "Bhome" });
        }
      }
    }
  },
  mounted() {
    if (localStorage.getItem("code")) {
      this.getDa(JSON.parse(localStorage.getItem("code")));
    }
  },
  methods: {
    wxLoginBtn() {
      this.getDa(this.$route.query.code);
      wxLogin().then((res) => {
        console.log(res);
      });
    },
    getDa(codeVavl) {
      let params = {
        code: codeVavl,
      };
      params = querystring.stringify(params);
      let a = localStorage.getItem("token");
      codeData(params).then((res) => {
        if (res.data.code !== 0) {
          localStorage.setItem("token", JSON.stringify(res.data.data.token));
          let userList = res.data.data.user;
          localStorage.setItem("user", JSON.stringify(userList));
          if (localStorage.getItem("user")) {
            let newUser = JSON.parse(localStorage.getItem("user"));
            if (newUser.roleCode === "user") {
              this.$router.push({ name: "Usehom" });
            }
            if (newUser.roleCode === "center") {
              this.$router.push({ name: "Ghome" });
            }
            if (newUser.roleCode === "salesman") {
              this.$router.push({ name: "Sahomepage" });
            }
            if (newUser.roleCode === "branch") {
              this.$router.push({ name: "Bhome" });
            }
          }
        } else {
          Toast(res.data.message);
        }
      });
    },
    Acclogin() {
      this.$router.push({ name: "Login" });
    },
  },
};
</script>
<style scoped  lang="scss" >
.partal {
  width: 100%;
  height: auto;
}
.partal_top {
  width: 100%;
  height: 2.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.42rem;
  color: #333333;
  font-weight: 500;
}
.partal_top img {
  width: 0.57rem;
  height: 0.56rem;
  margin-right: 0.15rem;
}
.partal_subject {
  width: 100%;
  height: 6.49rem;
  display: flex;
  justify-content: center;
}
.partal_subject img {
  width: 5.99rem;
  height: 6.49rem;
}
.caozuo {
  width: 100%;
  height: auto;
  margin-top: 0.8rem;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.caozuo .enter {
  width: 4.03rem;
  height: 0.95rem;
  background: #ffffff;
  border: 0.03rem solid #ffb400;
  border-radius: 48rpx;
  line-height: 95rpx;
  font-size: 0.38rem;
  color: #ffb400;
  padding: 0;
  margin-bottom: 0.35rem;
}
.caozuo span {
  font-size: 0.3rem;
  font-weight: 500;
  color: #999999;
}
</style>